#include <iostream>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

void error_exit(const char *msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}

#define NUM 1024

int main()
{
    // 对FIFO文件是否存在进行检测，如果存在则删除
    if (access("tp", F_OK) == 0)
    {
        if (unlink("tp") == -1)
            error_exit("unlink");
    }

    mkfifo("tp", 0664);
    int infd = open("test", O_RDONLY);
    if (infd == -1)
        error_exit("open");

    int outfd = open("tp", O_WRONLY);
    if (outfd == -1)
        error_exit("open");

    char buffer[NUM];
    int n;
    // 从test中读取数据到buffer中
    while ((n = read(infd, buffer, sizeof(buffer)-1)) > 0)
    {
        // 将读到的buffer中的数据写入管道tp中
        buffer[n]='\0';
        write(outfd, buffer, n);
    }

    close(infd);
    close(outfd);
    return 0;
}